<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
	<a-row>

    <a-col :span="12">
        <a-form-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name']" placeholder="请输入合同名称" :disabled="disableSubmit"></a-input>
        </a-form-item>
    </a-col>
    <a-col :span="12">
        <a-form-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['agreementCode']" placeholder="请输入合同编号" :disabled="disableSubmit"></a-input>
        </a-form-item>
    </a-col>
    <a-col :span="12">
        <a-form-item label="合同模板名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" :disabled="disableSubmit" @change="selectChange" v-decorator="['templateId']" :trigger-change="true" dictCode="ClmTemplate,name,id, statusId = 0" placeholder="请选择合同模板id"/>
        </a-form-item>
    </a-col>
    <a-col :span="12">
        <a-form-item label="签约时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择签约时间" :disabled="disableSubmit" v-decorator="['signDate']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
</a-col>
    <a-col :span="12">
        <a-form-item label="合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag :disabled="disableSubmit"  type="list" @change="selectType" v-decorator="['typeId', validatorRules.typeId]" :trigger-change="true" dictCode="DataDictionary,description,dictionaryCode,parentId = root&dictionaryTypeId = AgreementType" placeholder="请选择输入合同类型"/>
        </a-form-item>
    </a-col>
    <a-col :span="12">
      <a-form-item label="具体合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
        <j-dict-select-tag :disabled="disableSubmit" type="list" v-decorator="['specificTypeId']" :trigger-change="true" dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId = AgreementType, " placeholder="请选择输入具体合同类型"/>
      </a-form-item>
    </a-col>
    <a-col :span="24">
        <a-form-item label="甲方" :labelCol="{ xs: { span: 24 }, sm: { span: 4 }}" :wrapperCol="{ xs: { span: 24 }, sm: { span: 20 }}">
          <j-search-select-tag :disabled="disableSubmit" v-if="fromCompanyTypeId == 'PERSON'"
            :async="false"
            dict="Person,name,partyId,typeId=EMPLOYEE&legalStatusId=LEGAL_SUCCESS"
            v-decorator="['fromCompanyId']"
            :trigger-change="true"
            placeholder="请选择甲方"
          />

          <j-search-select-tag :disabled="disableSubmit"  v-else-if="fromCompanyTypeId == 'GROUP'"
            :async="false"
            dict="ClmCompany,companyName,id,legalStatusId=LEGAL_SUCCESS&statusId=2"
            v-decorator="['fromCompanyId']"
            :trigger-change="true"
            placeholder="请选择甲方"
          />
        </a-form-item>
    </a-col>
    <a-col :span="24">
        <a-form-item :disabled="disableSubmit" label="乙方" :labelCol="{ xs: { span: 24 }, sm: { span: 4 }}" :wrapperCol="{ xs: { span: 24 }, sm: { span: 20 }}">
          <j-search-select-tag v-if="toCompanyTypeId == 'PERSON'"
            :async="false"
            dict="Person,name,partyId,typeId=EMPLOYEE&legalStatusId=LEGAL_SUCCESS"
            v-decorator="['toCompanyId']"
            :trigger-change="true"
            placeholder="请选择乙方"
          />
          <j-search-select-tag :disabled="disableSubmit"  v-else-if="toCompanyTypeId == 'GROUP'"
            :async="false"
            dict="ClmCompany,companyName,id,legalStatusId=LEGAL_SUCCESS&statusId=2"
            v-decorator="['toCompanyId']"
            :trigger-change="true"
            placeholder="请选择乙方"
          />
        </a-form-item>
</a-col>


<a-col :span="12">
        <a-form-item label="合同生效时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date :disabled="disableSubmit" placeholder="请选择合同生效时间" v-decorator="['fromDate']" :trigger-change="true"  style="width: 100%"/>
        </a-form-item>
</a-col>
<a-col :span="12">
        <a-form-item label="合同到期时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date :disabled="disableSubmit" placeholder="请选择合同到期时间" v-decorator="['thruDate']" :trigger-change="true"  style="width: 100%"/>
        </a-form-item>
</a-col>
<a-col :span="12">
        <a-form-item label="合同金额(元)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number :disabled="disableSubmit" v-decorator="['agreementAmount']" placeholder="请输入合同金额(元)" style="width: 100%"/>
        </a-form-item>
</a-col>
  <a-col :span="12" v-for="item in itemList" :key="item.id">
        <a-form-item :label="item.name" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            v-if="item.fieldTypeId == '1'"
            v-model="item.description"
            :placeholder="'请输入' + item.name"
            :disabled="disableSubmit"
          ></a-input>
          <j-date
          :disabled="disableSubmit"
            v-if="item.fieldTypeId == '3'"
            :placeholder="'请选择' + item.name"
            v-model="item.description"
            :trigger-change="true"
            style="width: 100%"
          />
          <j-image-upload
            v-if="item.fieldTypeId == '5'"
            :disabled="disableSubmit"
            :isMultiple="false"
            v-model="item.description"
          ></j-image-upload>
          <!--
          <j-dict-select-tag type="list" v-model="item.description" v-if="item.fieldTypeId == '2'"
           :trigger-change="true" :dictCode="item.entityId+',description,dictionaryCode, dictionaryTypeId = '+item.dictionaryTypeId" :placeholder="'请选择'+item.name"/>
          -->
        </a-form-item>
  </a-col>

 </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction,getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JSearchSelectTag from '../../../../components/dict/JSearchSelectTag'
  import JImageUpload from '@/components/jeecg/JImageUpload'

  export default {
    name: "ClmAgreementModal",
    components: { 
      JDate,
      JDictSelectTag,
      JSearchSelectTag,
      JImageUpload
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        disableSubmit:false,
        visible: false,
        model: {},
        itemList: [],
        newItemList:[],
        ClmAgreementItem:[],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/online/special/api/createClmAgreement",
          edit: "/online/special/api/updateClmAgreement",
          get: "/online/api/getClmAgreement",
          getClmTemplateAndItem: "/online/api/getClmTemplateAndItem",
          selectDataDictionary:  "/online/api/selectDataDictionary",
        },
        fromCompanyTypeId:'GROUP',
        toCompanyTypeId:'GROUP',
        specificList: []
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
		    this.visible = true;
		    this.model = {};
        if(record.id){
	      httpAction(this.url.get,{id:record.id},"post").then((res)=>{
	         if(res.success){
		        this.model = Object.assign({}, res.result);
		        delete this.model['newItemList'];
		        this.newItemList = res.result.itemList;
		        if(this.newItemList){
		          let itemList = [];
              this.newItemList.forEach(item=>{
                itemList.push({fieldTypeId:item.fieldTypeId,description:item.description,name:item.name})
              })
              this.itemList = itemList;
            }
		        if(this.model.fromCompanyTypeId)this.fromCompanyTypeId = this.model.fromCompanyTypeId;
            if(this.model.toCompanyTypeId)this.toCompanyTypeId = this.model.toCompanyTypeId;
            console.log(this.fromCompanyTypeId);
		        this.$nextTick(() => {
		           this.form.setFieldsValue(pick(this.model,'name','agreementCode','templateId','typeId','specificTypeId','fromCompanyId','toCompanyId','signDate','agreementAmount','fromDate','thruDate','createDate','createBy','statusId'))
		        })   
	         }
	      })
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
              this.model.statusId = '0';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            if(this.fromCompanyTypeId)this.model.fromCompanyTypeId = this.fromCompanyTypeId;
            if(this.toCompanyTypeId)this.model.toCompanyTypeId = this.toCompanyTypeId;
            let formData = Object.assign(this.model, values);
            if(this.itemList){
                this.ClmAgreementItem = [];
                this.itemList.forEach(item=>{
                    this.ClmAgreementItem.push({name:item.name,description:item.description,fieldTypeId:item.fieldTypeId})
                })
                formData.ClmAgreementItem = this.ClmAgreementItem;
            }
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success('提交成功!');
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'name','agreementCode','templateId','typeId','specificTypeId','fromCompanyId','toCompanyId','signDate','agreementAmount','fromDate','thruDate','createDate','createBy','statusId'))
      },
      selectChange(e){
        getAction(this.url.getClmTemplateAndItem,{id: e}).then(res => {
            if(res.success){
              this.itemList = res.result.clmTemplateItemList
            }
        })
      },
      selectType(e){
        if(e == 'labor'){
            this.fromCompanyTypeId = 'GROUP';
            this.toCompanyTypeId = 'PERSON';
        }else{
            this.fromCompanyTypeId = 'GROUP';
            this.toCompanyTypeId = 'GROUP';
        }
        getAction(this.url.selectDataDictionary,{parentCode: e,dictionaryTypeId: 'AgreementType'}).then(res => {
            if(res.success){
              this.specificList = res.result.dictionaryList
            }
        })
      },
    }
  }
</script>
